package cn.javastudy.jdbc.advance;

import static cn.javastudy.jdbc.GlobalProperties.password;
import static cn.javastudy.jdbc.GlobalProperties.url;
import static cn.javastudy.jdbc.GlobalProperties.user;

import cn.javastudy.jdbc.advance.pojo.Employee;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ORM练习
 */
public class OrmPractise {
    public static void main(String[] args) throws SQLException {
        try(
            // 获取连接对象
            Connection connection = DriverManager.getConnection(url, user, password);
            // 执行SQL并获取结果集
            ResultSet resultSet = connection.prepareStatement("SELECT * FROM t_emp").executeQuery();
        ) {
            // 处理结果
            Employee employee = new Employee();
            while (resultSet.next()) {
                int empId = resultSet.getInt("emp_id");
                String empName = resultSet.getString("emp_name");
                Double empSalary = Double.valueOf(resultSet.getString("emp_salary"));
                int empAge = resultSet.getInt("emp_age");
                employee.setEmpId(empId);
                employee.setEmpName(empName);
                employee.setEmpSalary(empSalary);
                employee.setEmpAge(empAge);
                System.out.println(employee);
            }
        }
    }
}
